#include "Delegate.h"

void N2F::Iw3D::DefaultNotificationCallback(void *eventData, void *userData)
{
	return;
}

bool N2F::Iw3D::DefaultSystemCallback(void *eventData, void *userData)
{
	return(false);
}

N2F::Iw3D::Delegate::NotificationCallbackInfo::NotificationCallbackInfo(N2F::Iw3D::Delegate::NotificationCallback callback, void *data)
{
	this->callback = callback;
	this->userData = data;

	return;
}

N2F::Iw3D::Delegate::SystemCallbackInfo::SystemCallbackInfo(N2F::Iw3D::Delegate::SystemCallback callback, void *data)
{
	this->callback = callback;
	this->userData = data;

	return;
}

N2F::Iw3D::Delegate::Delegate()
{
	return;
}

N2F::Iw3D::Delegate::~Delegate()
{
	this->notificationEvents.clear();
	this->systemEvents.clear();

	return;
}

bool N2F::Iw3D::Delegate::hookNotificationEvent(int key, NotificationCallback callback, void *userData)
{
	for (NotificationListIter it = this->notificationEvents.begin(); it != this->notificationEvents.end(); ++it)
	{
		if ((*it).first == key)
		{
			this->notificationEvents.insert(NotificationListItem((*it).first, NotificationCallbackInfo(callback, userData)));

			return(true);
		}
	}

	return(false);
}

bool N2F::Iw3D::Delegate::hookSystemEvent(int key, SystemCallback callback, void *userData)
{
	for (SystemListIter it = this->systemEvents.begin(); it != this->systemEvents.end(); ++it)
	{
		if ((*it).first == key)
		{
			(*it).second = SystemCallbackInfo(callback, userData);

			return(true);
		}
	}

	return(false);
}

bool N2F::Iw3D::Delegate::registerNotificationEvent(int key)
{
	for (NotificationListIter it = this->notificationEvents.begin(); it != this->notificationEvents.end(); ++it)
	{
		if ((*it).first == key)
		{
			return(false);
		}
	}

	this->notificationEvents.insert(NotificationListItem(key, NotificationCallbackInfo(DefaultNotificationCallback, NULL)));

	return(true);
}

bool N2F::Iw3D::Delegate::registerSystemEvent(int key)
{
	for (SystemListIter it = this->systemEvents.begin(); it != this->systemEvents.end(); ++it)
	{
		if ((*it).first == key)
		{
			return(false);
		}
	}

	this->systemEvents.insert(SystemListItem(key, SystemCallbackInfo(DefaultSystemCallback, NULL)));

	return(true);
}

void N2F::Iw3D::Delegate::touchNotificationEvent(int key, void *eventData)
{
	for (NotificationListIter it = this->notificationEvents.begin(); it != this->notificationEvents.end(); ++it)
	{
		if ((*it).first == key)
		{
			(*it).second.callback(eventData, (*it).second.userData);
		}
	}

	return;
}

void N2F::Iw3D::Delegate::touchSystemEvent(int key, void *eventData)
{
	for (SystemListIter it = this->systemEvents.begin(); it != this->systemEvents.end(); ++it)
	{
		if ((*it).first == key)
		{
			(*it).second.callback(eventData, (*it).second.userData);

			break;
		}
	}

	return;
}
